/**
 * Belongs to WEF
 */

function smanPromptList(divName, arrList, arrCodeList, objInputId,
		objInputValue) {
	this.style = "background:#E8F7EB;border: 1px solid #CCCCCC;font-size:14px;cursor: default;"
	if (arrList.constructor != Array) {
		alert('smanPromptList初始化失败:第一个参数非数组!');
		return;
	}
	function createOptions() {
		var objouter = document.getElementById(divName) // 显示的DIV对象
		var objInput = document.getElementById(objInputId); // 显示VALUE文本框对象
		var objInputCode = document.getElementById(objInputValue); // 隐藏CODE文本框对象
		var selectedIndex = -1;
		var intTmp; // 循环用的
		if (objInput == null) {
			alert('smanPromptList初始化失败:没有找到"' + objInputId + '"文本框');
			return;
		}
		// 文本框失去焦点
		objInput.onblur = function() {
			objouter.style.display = 'none';
		}
		// 文本框按键抬起
		objInput.onkeyup = checkKeyCode;
		// 文本框得到焦点
		// objInput.onfocus=checkAndShow;
		function checkKeyCode(event) {
			var e = event || window.event;
			var ie = (document.all) ? true : false
			if (ie) {
				var keyCode = e.keyCode;
				if (keyCode == 40 || keyCode == 38) { // 下上
					var isUp = false
					if (keyCode == 40)
						isUp = true;
					chageSelection(isUp)
				} else if (keyCode == 13) {// 回车
					outSelection(selectedIndex);
				} else {
					checkAndShow();
				}
			} else {
				var keyCode = e.keyCode;// alert(keyCode);
				if (keyCode == 40 || keyCode == 38) { // 下上
					var isUp = false
					if (keyCode == 40)
						isUp = true;
					chageSelection(isUp)
				} else if (keyCode == 13) {// 回车
					outSelection(selectedIndex);
				} else {
					checkAndShow();
				}
			}
			divPosition();
		}

		function checkAndShow() {
			var strInput = objInput.value
			if (strInput != "") {
				divPosition();
				selectedIndex = -1;
				objouter.innerHTML = "";
				var i = 1;
				for (intTmp = 0; intTmp < arrList.length; intTmp++) {
					if (arrList[intTmp].substr(0, strInput.length)
							.toUpperCase() == strInput.toUpperCase()) {
						addOption(arrList[intTmp], arrCodeList[intTmp], i);
						i++;
					}
				}
				objouter.style.display = '';
			} else {
				objouter.style.display = 'none';
			}
			// 添加层
			function addOption(value, code, i) {
				var bgcolor;
				var div_width = objInput.style.width == '' ? "118px" : objInput.style.width;
				if(i % 2 == 0){
					bgcolor = "sman_Style";
				}else{
					bgcolor = "sman_Style1";
				}
				objouter.innerHTML += "<div class='"+bgcolor+"' style='width:" + div_width +"' onmouseover=\"this.className='sman_selectedStyle'\" "
						+ "onmouseout=\"this.className='"+bgcolor+"'\" onmousedown=\"document.getElementById('"
						+ objInputId
						+ "').value='"
						+ value
						+ "';document.getElementById('"
						+ objInputValue
						+ "').value='" + code + "'\">" + value + "</div>"
			}
		}
		function chageSelection(isUp) {
			if (objouter.style.display == 'none') {
				objouter.style.display = '';
			} else {
				if (isUp)
					selectedIndex++
				else
					selectedIndex--
			}
			var maxIndex = objouter.children.length - 1;
			if (selectedIndex < 0) {
				selectedIndex = 0
			}
			if (selectedIndex > maxIndex) {
				selectedIndex = maxIndex
			}
			for (intTmp = 0; intTmp <= maxIndex; intTmp++) {
				
				if (intTmp == selectedIndex) {
					objouter.children[intTmp].className = "sman_selectedStyle";
				} else {
					objouter.children[intTmp].className = intTmp%2==0?'sman_Style1':'sman_Style';
				}
			}
		}
		// 回车选中赋值
		function outSelection(Index) {
			if (Index != -1) {
				objInput.value = objouter.children[Index].innerText;
			}
			for (var i = 0; i < arrList.length; i++) {
				if (arrList[i] == objInput.value) {
					objInputCode.value = arrCodeList[i];
					break;
				}
			}
			objouter.style.display = 'none';
		}
		// 创建层时定位层的位置
		function divPosition() {
			setElementPosition(objouter, objInput);
		}
	}
	
	var div_width = document.getElementById(objInputId).style.width == '' ? "118px" : document.getElementById(objInputId).style.width;

	if(document.all){
		document.write("<div id='" + divName
				+ "' style='width:"+div_width+";position:absolute;z-index:1;display:none;margin:20px 0 0;" + this.style
				+ "' onbulr></div>");
	}else{
		document.write("<div id='" + divName
				+ "' style='width:"+div_width+";position:absolute;z-index:1;display:none;" + this.style
				+ "' onbulr></div>");
	}
	document.write("<style>.sman_selectedStyle{background-Color:#102681;color:#FFFFFF}</style>");
	document.write("<style>.sman_Style{background-Color:#E8F7EB;color:#000000}</style>");
	document.write("<style>.sman_Style1{background-Color:white;color:#000000}</style>");
	createOptions();
	function getAbsoluteHeight(ob) {
		return ob.offsetHeight
	}
	function getAbsoluteWidth(ob) {
		return ob.offsetWidth
	}
	function getAbsoluteLeft(ob) {
		var mendingLeft = ob.offsetLeft;
		while (ob != null && ob.offsetParent != null
				&& ob.offsetParent.tagName != "BODY") {
			mendingLeft += ob.offsetParent.offsetLeft;
			ob = ob.offsetParent;
		}
		return mendingLeft;
	}
	function getAbsoluteTop(ob) {
		var mendingTop = ob.offsetTop;
		while (ob != null && ob.offsetParent != null
				&& ob.offsetParent.tagName != "BODY") {
			mendingTop += ob.offsetParent.offsetTop;
			ob = ob.offsetParent;
		}
		return mendingTop;
	}
	
	//  DOM没有提供insertAfter()方法，所以我们只能自己写一个。
	function insertAfter(newElement,targetElement) {
	  var parent = targetElement.parentNode;
	  if (parent.lastChild == targetElement) {
		// 如果最后的节点是目标元素，则直接添加。因为默认是最后
	    parent.appendChild(newElement);
	  } else {
	    parent.insertBefore(newElement,targetElement.nextSibling);
		//如果不是，则插入在目标元素的下一个兄弟节点 的前面。也就是目标元素的后面。
	  }
	}
	
	function setElementPosition(newElement,targetElement){
        insertAfter(newElement, targetElement);
        newElement.style.left = getAbsoluteLeft(targetElement);
		newElement.style.width = getAbsoluteWidth(targetElement);
	}
}